<template>
  <el-dialog
    :title="!dataForm.no ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="设备编号" prop="deviceId">
      <el-input v-model="dataForm.deviceId" placeholder="设备编号"></el-input>
    </el-form-item>
    <el-form-item label="设备名称" prop="deviceName">
      <el-input v-model="dataForm.deviceName" placeholder="设备名称"></el-input>
    </el-form-item>
      <el-form-item label="设备类型" prop="deviceType">
        <el-input v-model="dataForm.deviceType" placeholder="设备类型"></el-input>
      </el-form-item>
    <el-form-item label="设备配置" prop="deviceProfileName">
      <el-input v-model="dataForm.deviceProfileName" placeholder="设备配置"></el-input>
    </el-form-item>
    <el-form-item label="协议类型" prop="deviceProtocol">
      <el-input v-model="dataForm.deviceProtocol" placeholder="协议类型"></el-input>
    </el-form-item>
    <el-form-item label="时间" prop="createdTime">
      <el-input v-model="dataForm.createdTime" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          no: 0,
          deviceId: '',
          deviceName: '',
          deviceType: '',
          deviceProfileName: '',
          deviceProtocol: '',
          createdTime: ''
        },
        dataRule: {
          deviceId: [
            { required: true, message: '设备ID不能为空', trigger: 'blur' }
          ],
          deviceName: [
            { required: true, message: '鐠佹儳顦崥宥囆?不能为空', trigger: 'blur' }
          ],
          deviceType: [
            { required: true, message: '品牌不能为空', trigger: 'blur' }
          ],
          deviceProfileName: [
            { required: true, message: '设备类型不能为空', trigger: 'blur' }
          ],
          deviceProtocol: [
            { required: true, message: '用途不能为空', trigger: 'blur' }
          ],
          createdTime: [
            { required: true, message: '接口类型不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.no = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.no) {
            console.log(this.dataForm.no)
            this.$http({
              url: this.$http.adornUrl(`/cuttingcur/cuttingmachinecurdata/getCuttingInfoById`),
              method: 'get',
              params: this.$http.adornParams({
                'deviceId': this.dataForm.no
              })
            }).then(({data}) => {
              console.log(data[0].deviceName)
              if (data) {
                this.dataForm.deviceId = data[0].deviceId
                this.dataForm.deviceName = data[0].deviceName
                this.dataForm.deviceType = data[0].deviceType
                this.dataForm.deviceProfileName = data[0].deviceProfileName
                this.dataForm.deviceProtocol = data[0].deviceProtocol
                this.dataForm.createdTime = data[0].createdTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            console.log(this.dataForm)
            this.$http({
              url: this.$http.adornUrl(`/cuttingcur/cuttingmachinecurdata/updateByDeviceId`),
              method: 'post',
              data: this.$http.adornData({
                'deviceId': this.dataForm.deviceId,
                'deviceName': this.dataForm.deviceName,
                'deviceType': this.dataForm.deviceType,
                'deviceProfileName': this.dataForm.deviceProfileName,
                'deviceProtocol': this.dataForm.deviceProtocol,
                'createdTime': this.dataForm.createdTime
              })
            }).then(({data}) => {
              console.log(data)
              if (data) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
